package com.example.hissys.mapper;

import com.example.hissys.entity.DoctorSchedule;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;
import java.util.List;

@Mapper
public interface ScheduleMapper {
    /**
     * 根据医生ID查询排班信息
     */
    @Select("SELECT id, doctor_id, DATE_FORMAT(schedule_date, '%Y-%m-%d') as schedule_date, " +
            "time_period, available_slots " +
            "FROM doctor_schedule " +
            "WHERE doctor_id = #{doctorId} " +
            "AND schedule_date >= CURDATE() " +
            "ORDER BY schedule_date, time_period")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "timePeriod", column = "time_period"),
        @Result(property = "availableSlots", column = "available_slots")
    })
    List<DoctorSchedule> findByDoctorId(Integer doctorId);

    /**
     * 根据医生ID和日期查询排班信息
     */
    @Select("SELECT id, doctor_id, DATE_FORMAT(schedule_date, '%Y-%m-%d') as schedule_date, " +
            "time_period, available_slots " +
            "FROM doctor_schedule " +
            "WHERE doctor_id = #{doctorId} " +
            "AND DATE(schedule_date) = DATE(#{date}) " +
            "ORDER BY time_period")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "timePeriod", column = "time_period"),
        @Result(property = "availableSlots", column = "available_slots")
    })
    List<DoctorSchedule> findByDoctorIdAndDate(@Param("doctorId") Integer doctorId, @Param("date") String date);

    /**
     * 检查排班是否存在且有号源
     */
    @Select("SELECT id, doctor_id, DATE_FORMAT(schedule_date, '%Y-%m-%d') as schedule_date, " +
            "time_period, available_slots " +
            "FROM doctor_schedule " +
            "WHERE doctor_id = #{doctorId} " +
            "AND DATE(schedule_date) = DATE(#{date}) " +
            "AND time_period = #{timePeriod} " +
            "AND available_slots > 0")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "timePeriod", column = "time_period"),
        @Result(property = "availableSlots", column = "available_slots")
    })
    DoctorSchedule checkAvailability(@Param("doctorId") Integer doctorId, 
                                   @Param("date") String date, 
                                   @Param("timePeriod") String timePeriod);

    /**
     * 更新可用号源数
     */
    @Update("UPDATE doctor_schedule " +
            "SET available_slots = available_slots - 1 " +
            "WHERE doctor_id = #{doctorId} " +
            "AND DATE(schedule_date) = DATE(#{date}) " +
            "AND time_period = #{timePeriod} " +
            "AND available_slots > 0")
    int updateAvailableSlots(@Param("doctorId") Integer doctorId, 
                            @Param("date") String date, 
                            @Param("timePeriod") String timePeriod);
} 